package cn.paging.action;

import java.util.Iterator;

import org.apache.struts2.ServletActionContext;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;


import com.opensymphony.xwork2.ActionSupport;

import cn.paging.bean.PageBean;
import cn.paging.model.Member;
import cn.paging.service.MemberService;
import cn.paging.util.HibernateUtils;

public class ListMemberAction extends ActionSupport
{
	private MemberService memberService;
	private Member member;
	private String id;
	private static int myId;//keep the id from the link 
	
	public void setMemberService(MemberService memberService)
	{
		this.memberService = memberService;
	}
	
	public Member getMember()
	{
		return member;
	}

	public void setMember(Member member)
	{
		this.member = member;
	}

	private int page; //initial page,default page=1

	private PageBean pageBean; //pages information

	public int getPage()
	{
		return page;
	}

	public void setPage(int page)
	{ 
		this.page = page;
	}

	public PageBean getPageBean()
	{
		return pageBean;
	}

	public void setPageBean(PageBean pageBean)
	{
		this.pageBean = pageBean;
	}
	
	public String getId()
	{
		return id;
	}

	public void setId(String id)
	{
		this.id = id;
	}

	/**
	 * list paged records
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception
	{
		System.out.println("page="+page);
		//list the current page's records
		this.pageBean = memberService.queryForPage(3, page);
		return SUCCESS;
	}
	
	/**
	 * edit member for update
	 * @return string
	 * @throws Exception
	 */
	public String edit() throws Exception
	{
		myId=Integer.parseInt(id);
		member = (Member)memberService.edit(Integer.parseInt(id));
		
		return SUCCESS;
	}
	
	/**
	 * update member
	 * @return string
	 * @throws Exception
	 */
	public String update() throws Exception
	{
		Member m = new Member();
		m.setId(myId);
		m.setName(member.getName());
		m.setPassword(member.getPassword());
		memberService.update(m);
		return SUCCESS;
	}
	
	/**
	 * add member
	 * @return string
	 * @throws Exception
	 */
	public String add() throws Exception
	{
		Member m = new Member();
		m.setName(member.getName());
		m.setPassword(member.getPassword());
		memberService.add(m);
		return SUCCESS;
	}
	
	
	
	/**
	 * delete member
	 * @return string
	 * @throws Exception
	 */
	public String delete()throws Exception
	{
		memberService.delete(Integer.parseInt(id));
		return SUCCESS;
		
	}

}
